package com.aia.aia2.service;

import org.apache.log4j.Logger;
import org.springframework.util.Assert;

import com.aia.aia2.domain.FormdataExt;
import com.aia.aia2.domain.Gpinsu;

public class Counter {
	private static final Logger logger = Logger.getLogger(Counter.class);
	public static final int ARG_NUM = 16;
	private FormdataExt formdata;
	private Gpinsu gpinsu;

	public Counter(FormdataExt formdata, Gpinsu gpinsu) {
		super();
		this.formdata = formdata;
		this.gpinsu = gpinsu;
	}

	public void count() throws CounterException {
		logger.debug("count() start");
		doValidate();
		String arguments = gpinsu.getGI_ARGUMENTS();
		Assert.notNull(arguments, "参数配置错误[no config]");
		String[] argumentsArray = arguments.split("\\|");
		Assert.notNull(argumentsArray, "参数配置错误[empty]");
		Assert.isTrue(argumentsArray.length == 16, "参数配置错误[num16]");
		int[] argumentsInt = new int[ARG_NUM];
		try {
			for (int i = 0; i < ARG_NUM; i++) {
				argumentsInt[i] = Integer.valueOf(argumentsArray[i]);
				Assert.isTrue(argumentsInt[i] > 0, "参数配置错误[zero]");
			}
		} catch (Exception e) {
			throw new RuntimeException("参数配置错误[convert]", e);
		}
		long planCode = formdata.getFD_PLAN();
		long amount = 0;// 计算结果
		StringBuffer inputSb = new StringBuffer();
		StringBuffer descSb = new StringBuffer();
		int indexMoved = 0;
		if (!(planCode == 2)) {
			planCode = 1;
			formdata.setFD_PLAN(planCode);
			indexMoved = 0;
			inputSb.append("1|");
			descSb.append("选择了计划1;");
		} else {
			indexMoved = 1;
			inputSb.append("2|");
			descSb.append("选择了计划2;");
		}
		amount += argumentsInt[0 + indexMoved] * formdata.getCNTA1();
		amount += argumentsInt[2 + indexMoved] * formdata.getCNTB1();
		amount += argumentsInt[4 + indexMoved] * formdata.getCNTA2();
		amount += argumentsInt[6 + indexMoved] * formdata.getCNTB2();
		
		amount += argumentsInt[8 + indexMoved] * formdata.getCNTA3();
		amount += argumentsInt[10 + indexMoved] * formdata.getCNTB3();
		amount += argumentsInt[12 + indexMoved] * formdata.getCNTA4();
		amount += argumentsInt[14 + indexMoved] * formdata.getCNTB4();
		
		inputSb.append(formdata.getCNTA1() + "|" + formdata.getCNTB1() + "|");
		inputSb.append(formdata.getCNTA2() + "|" + formdata.getCNTB2() + "|");
		inputSb.append(formdata.getCNTA3() + "|" + formdata.getCNTB3() + "|");
		inputSb.append(formdata.getCNTA4() + "|" + formdata.getCNTB4() + "|");
		
		descSb.append("总人数" + formdata.getFD_COUNT() + "人;保费"+amount+"元;");
		
		descSb.append("16-30岁有" + formdata.getCNT1() + "人");
		descSb.append("(职业类别1:" + formdata.getCNTA1() + "人," + argumentsInt[0 + indexMoved] + "元,");
		descSb.append("职业类别2:" + formdata.getCNTB1() + "人," + argumentsInt[2 + indexMoved] + "元,);");
		
		descSb.append("31-40岁有" + formdata.getCNT2() + "人");
		descSb.append("(职业类别1:" + formdata.getCNTA2() + "人," + argumentsInt[4 + indexMoved] + "元,");
		descSb.append("职业类别2:" + formdata.getCNTB2() + "人," + argumentsInt[6 + indexMoved] + "元,);");
		
		descSb.append("41-50岁有" + formdata.getCNT3() + "人");
		descSb.append("(职业类别1:" + formdata.getCNTA3() + "人," + argumentsInt[8 + indexMoved] + "元,");
		descSb.append("职业类别2:" + formdata.getCNTB3() + "人," + argumentsInt[10 + indexMoved] + "元,);");
		
		descSb.append("51-55岁有" + formdata.getCNT4() + "人");
		descSb.append("(职业类别1:" + formdata.getCNTA4() + "人," + argumentsInt[12 + indexMoved] + "元,");
		descSb.append("职业类别2:" + formdata.getCNTB4() + "人," + argumentsInt[14 + indexMoved] + "元,);");
		
		
		logger.debug("计算结果:" + amount);
		logger.debug("计算参数:" + inputSb.toString());
		logger.debug("计算参数描述:" + descSb.toString());
		
		formdata.setFD_RESULT(amount);
		formdata.setFD_INPUT(inputSb.toString());
		formdata.setFD_INPUTDESC(descSb.toString());
		logger.debug("count() end");
	}

	private void doValidate() throws CounterException {
		int countShouldBe = 0;
		countShouldBe += formdata.getCNT1();
		countShouldBe += formdata.getCNT2();
		countShouldBe += formdata.getCNT3();
		countShouldBe += formdata.getCNT4();
		if (countShouldBe != formdata.getFD_COUNT()) {
			String msg = "输入的总人数不一致：您输入的总人数为" + formdata.getFD_COUNT() + "人,四个年龄分布的人数相加为" + countShouldBe + "人";
			throw new CounterException(msg);
		}
		{
			int ageCountShouldBe = 0;
			ageCountShouldBe += formdata.getCNTA1();
			ageCountShouldBe += formdata.getCNTB1();
			if (ageCountShouldBe != formdata.getCNT1()) {
				String msg = "输入的人数不一致：您输入的16-30岁人数为" + formdata.getCNT1() + "人,2个职业分布的人数相加为" + ageCountShouldBe + "人";
				throw new CounterException(msg);
			}
		}
		{
			int ageCountShouldBe = 0;
			ageCountShouldBe += formdata.getCNTA2();
			ageCountShouldBe += formdata.getCNTB2();
			if (ageCountShouldBe != formdata.getCNT2()) {
				String msg = "输入的人数不一致：您输入的31-40岁人数为" + formdata.getCNT2() + "人,2个职业分布的人数相加为" + ageCountShouldBe + "人";
				throw new CounterException(msg);
			}
		}
		{
			int ageCountShouldBe = 0;
			ageCountShouldBe += formdata.getCNTA3();
			ageCountShouldBe += formdata.getCNTB3();
			if (ageCountShouldBe != formdata.getCNT3()) {
				String msg = "输入的人数不一致：您输入的41-50岁人数为" + formdata.getCNT3() + "人,2个职业分布的人数相加为" + ageCountShouldBe + "人";
				throw new CounterException(msg);
			}
		}
		{
			int ageCountShouldBe = 0;
			ageCountShouldBe += formdata.getCNTA4();
			ageCountShouldBe += formdata.getCNTB4();
			if (ageCountShouldBe != formdata.getCNT4()) {
				String msg = "输入的人数不一致：您输入的51-55岁人数为" + formdata.getCNT4() + "人,2个职业分布的人数相加为" + ageCountShouldBe + "人";
				throw new CounterException(msg);
			}
		}

	}

}
